package com.luych.toolbox.data.service.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.luych.toolbox.common.constant.PageConstant;
import com.luych.toolbox.common.feign.intf.database.service.DataProjectFeign;
import com.luych.toolbox.common.feign.param.data.service.DataProjectInitParam;
import com.luych.toolbox.common.feign.param.data.service.DataProjectParam;
import com.luych.toolbox.common.feign.param.data.service.DataProjectUpgradeParam;
import com.luych.toolbox.common.feign.view.data.service.DataProjectTimeLineView;
import com.luych.toolbox.common.feign.view.data.service.DataProjectView;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import com.luych.toolbox.data.service.entity.DataProject;
import com.luych.toolbox.data.service.service.DataProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "数据代理")
@RestController
@RequestMapping("/dp")
public class DataProjectController
        extends BaseController<DataProjectView, DataProjectParam, DataProject>
        implements DataProjectFeign {

    private final DataProjectService dataProjectService;

    @Autowired
    public DataProjectController(DataProjectService dataProjectService) {
        super(dataProjectService);
        this.dataProjectService = dataProjectService;
    }

    @ApiOperation("按关键字查询数据代理信息")
    @GetMapping("/search/keyword")
    public CommonPageResult<DataProjectView> pageByKeyword(
            @ApiParam(value = "每页容量") @RequestHeader(name = "pageSize", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE_STR) long pageSize,
            @ApiParam(value = "查询页码") @RequestHeader(name = "pageNumber", required = false, defaultValue = PageConstant.DEFAULT_PAGE_NUMBER_STR) long pageNumber,
            @ApiParam(value = "排序字段") @RequestHeader(name = "sort", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SORT) String sort,
            @ApiParam(value = "正序/倒序，取值ASC/DESC") @RequestHeader(name = "order", required = false, defaultValue = PageConstant.DEFAULT_PAGE_ORDER) String order,
            @ApiParam(value = "查询关键字，支持按名称模糊查询") @RequestParam(name = "keyword", required = false) String keyword
    ) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<DataProjectView> pageResult = dataProjectService.pageByKeyword(commonPageParam, keyword);
        return CommonPageResult.getInstance(pageResult);
    }

    @ApiOperation("启用")
    @PatchMapping("/{id}/enable")
    public void setEnable(@ApiParam(value = "ID") @PathVariable String id) {
        dataProjectService.setEnable(id);
    }

    @ApiOperation("停用")
    @PatchMapping("/{id}/disable")
    public void setDisable(@ApiParam(value = "ID") @PathVariable String id) {
        dataProjectService.setDisable(id);
    }

    @ApiOperation("初始化")
    @PatchMapping("/{id}/init")
    public void doInit(@ApiParam(value = "ID") @PathVariable String id,
                       @ApiParam(value = "参数") @RequestBody DataProjectInitParam param) {
        dataProjectService.doInit(id, param);
    }

    @ApiOperation("升级")
    @PatchMapping("/{id}/upgrade")
    public void doUpgrade(@ApiParam(value = "ID") @PathVariable String id,
                          @ApiParam(value = "参数") @RequestBody DataProjectUpgradeParam param) {
        dataProjectService.doUpgrade(id, param);
    }

    @ApiOperation("时间线")
    @GetMapping("/{id}/timeline")
    public List<DataProjectTimeLineView> timelines(@ApiParam(value = "ID") @PathVariable String id) throws JsonProcessingException {
        return dataProjectService.timelines(id);
    }
}
